package org.itboys.seller.service;

import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.seller.entity.ChangeScoreRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 商家登陆账号
 * @author weisky
 *
 */
@Service
public class ChangeScoreRecordService extends BaseService<ChangeScoreRecord, Long>{
	
	private static final long serialVersionUID = 1L;

	@Resource(name = "sellerDS")
	private MongoDataSource ds;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<ChangeScoreRecord> getEntityClass() {
		return ChangeScoreRecord.class;
	}

    public int getOfferScoreOneDay(long sourceUserId, long toUserId) {
        long endTime = TimeUtils.getDayFirstTime(new Date()).getTime();
        long beginTime = endTime - 24 * 3600 * 1000;
        List<ChangeScoreRecord> recordList = ds.createQuery(ChangeScoreRecord.class)
                .field("resourceUserId").equal(sourceUserId)
                .field("userId").equal(toUserId)
                .field("auditState").equal(1)
                .field("type").equal(1)
                .field("ct").greaterThanOrEq(beginTime)
                .field("ct").lessThan(endTime).asList();
        return recordList.stream().mapToInt(ChangeScoreRecord::getScoreDelta).sum();
    }

    public List<ChangeScoreRecord> getOfferScoreRecordsOneDay(long userId) {
        long endTime = TimeUtils.getDayFirstTime(new Date()).getTime();
        long beginTime = endTime - 24 * 3600 * 1000;
        return ds.createQuery(ChangeScoreRecord.class)
                .field("userId").equal(userId)
                .field("resourceUserId").notEqual(userId)
                .field("auditState").equal(1)
                .field("type").equal(1)
                .field("ct").greaterThanOrEq(beginTime)
                .field("ct").lessThan(endTime).asList();
    }

    public List<ChangeScoreRecord> getAll() {
        return ds.createQuery(ChangeScoreRecord.class).asList();
    }

}
